package com.samphanie.auiu.auth.controller;

import com.samphanie.auiu.auth.service.IRoleService;
import com.samphanie.auiu.common.dto.RoleDto;
import com.samphanie.auiu.common.utils.ApiResponse;
import com.samphanie.auiu.mbg.model.SysRole;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @author ZSY
 * @email 1451691457@qq.com
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/role")
public class RoleController {

    private final IRoleService roleService;

    /**
     * 查询角色列表
     */
    @GetMapping("/list")
    public ApiResponse<?> listRoleList() {
        List<RoleDto> roleList = roleService.listRoleList();
        return ApiResponse.success(roleList);
    }

    /**
     * 获取角色详细信息
     */
    @GetMapping("/{id}")
    public ApiResponse<?> getRoleInfo(@PathVariable("id") Long id) {
        return ApiResponse.success(roleService.getById(id));
    }

    /**
     * 新增角色
     */
    @PostMapping("/add")
    public ApiResponse<?> addRole(@Valid @RequestBody RoleDto role) {
        boolean result = roleService.add(role);
        return ApiResponse.success(result);
    }

    /**
     * 修改角色
     */
    @PutMapping("/update")
    public ApiResponse<?> updateRole(@RequestBody SysRole role) {

        return ApiResponse.success(roleService.updateById(role));
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/delete")
    public ApiResponse<?> removeRoles(@RequestBody Long[] ids) {
        return ApiResponse.success(roleService.removeByIds(Arrays.asList(ids)));
    }

}
